package client;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.ObjectInputStream;
import java.net.Socket;

public class TokenClient {
	private Socket client;
	private DataInputStream fromServer;
	private DataOutputStream toServer;
	private ObjectInputStream objFromServer;
	private String username = "hqthai01";
	private int pin = 77641;
	
	public TokenClient(String host, int port){
		try{
			client = new Socket(host, port);
			fromServer = new DataInputStream(client.getInputStream());
			toServer = new DataOutputStream(client.getOutputStream());
			objFromServer = new ObjectInputStream(client.getInputStream());
			System.out.println(fromServer.readUTF());
		}catch(Exception ex){
			ex.printStackTrace();
		}
	}
	
	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public int getPin() {
		return pin;
	}

	public void setPin(int pin) {
		this.pin = pin;
	}

	public void tokenGenerator(){
		try {
			boolean connected = true;
			int token = 0;
			while(connected){
				toServer.writeUTF("GET-TOKEN");
				toServer.writeUTF(username);
				toServer.writeInt(pin);
				token = fromServer.readInt();
				System.out.println(token);
				Thread.sleep(60000);
			}
			objFromServer.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public int getToken(){
		int token = -1;
		try {
			toServer.writeUTF("GET-TOKEN");
			toServer.writeUTF(username);
			toServer.writeInt(pin);
			token = fromServer.readInt();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return token;
	}
	
	public static void main(String[] args) {
		new TokenClient("localhost", 7764).tokenGenerator();
	}
	
}
